<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class MY_Controller extends CI_Controller {
	
	function __construct() {
		parent::__construct();
		error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
	}
	
	
	/**
	 * 
	 * Throw control to different action (control and module if was specified).
	 * @param string $method
	 * @param array $params
	 */
	function _forward($method,array $params = array()){
		if (method_exists($this,$method)){
			return call_user_func_array(array($this,$method),$params);
		}
		show_404("Controller Method  failed to run");
	}
	/**
	 * 
	 * Set redirect into response
	 * @param string $uri
	 * @param string	the method: location or redirect
	 */
	function _redirect($uri,$method = 'location'){
		redirect($uri,$method);
	}
	
	function get_full_action(){
		return $this->router->fetch_module.'_'
			.$this->router->fetch_class.'_'
			.$this->router->fetch_method;
	}
	
	/**
     * Translate a phrase
     *
     * @return string
     */
	function __(){
		$args = func_get_args();
		return $this->lang->translate($args);
	}
	
	/**
     * Declare headers and content file in response for file download
     *
     *@param string $file_name
     *@param string $contentType
     *@return MY_Controller
     */
	
	function dowload_respone($file_name,$contentType = 'application/octet-stream'){
		clearstatcache();
		$file_without_path = basename($file_name);
		$abs_file =  str_replace('/',DS,$file_name);
		
		ob_end_clean();
		header("Cache-Control: public, must-revalidate");
		header('Cache-Control: pre-check=0, post-check=0, max-age=0');
		header("Pragma: no-cache");
		header("Expires: 0"); 
		header("Content-Description: File Transfer");
		header("Accept-Ranges: bytes");
		header("Content-Type: " . (string)$contentType);
		header('Content-Disposition: attachment; filename="'.$file_without_path.'"');
		header("Content-Transfer-Encoding: binary\n");
		// Try to deliver in chunks
		@set_time_limit(0);
		$fp = @fopen($file_name, 'rb');
		if ($fp !== false) {
			while (!feof($fp)) {
				echo fread($fp, 8192);
			}
			fclose($fp);
		} else {
			@readfile($file_name);
		}
		flush();
		exit;
	}
}